My first sage notebook¶
This is an annotated version of the notebook that was created in class on Aug 28, 2024.
First, we discussed that this is a Jupyter notebook, and it is running a SageMath kernel meaning that programming cells are run through SageMath.
These text cells are written in Markdown. We discussed some aspects of Markdown in class, and you'll learn a bit in homework zero.
Note that in the version of Markdown in a notebook we can also include latex, by enclosing the LaTeX code in dollar signs. For example: $$\sum_{n=1}^\infty \frac{1}{n} = +\infty.$$ For this, you only need to understand the mathematical syntax of LaTeX. To learn this, you could start in this tutorial.
Markdown Learning Resources:
Markdown Guide
A comprehensive resource for Markdown syntax, tools, and tips.Markdown Tutorial
An interactive tutorial for learning Markdown basics and advanced techniques.“Markdown Cheatsheet”
A handy cheatsheet for quick reference to Markdown syntax.
LaTeX Learning Resources
-
- Official website with comprehensive documentation and tutorials.
-
- A quick and practical guide to getting started with LaTeX.
The Not So Short Introduction to LaTeX2e
- A classic and detailed introduction to LaTeX, available as a free PDF.
-
- A collection of tutorials covering various aspects of LaTeX, from basic to advanced topics.
Using SageMath as a calculator¶
First, you can use Sage to do simple calculations. For example:
5 + 4
9
The output above is indicating that the result of the calculation 5 + 4
is 9
.
Unlike most calculators, Sage does exact calculations with integers of arbitrary size:
2^100
1267650600228229401496703205376
2^1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
It also correctly handles operatorator precidence. For example, the calculation below evaluates the expression as we are taught in school:
7 * (4+ 3/2)
77/2
Note that an exact result is returned. SageMath also performs exact computations with rational numbers of arbitrary size.
There are two competing notations for exponentiation, as shown below:
4^2
16
2**5
32
As a heads up: The version **
also works in Python (the language upon which SageMath is built), but ^
does not.
Exponentiation also works for rational numbers:
(2/3)^1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376/1322070819480806636890455259752144365965422032752148167664920368226828597346704899540778313850608061963909777696872582355950954582100618911865342725257953674027620225198320803878014774228964841274390400117588618041128947815623094438061566173054086674490506178125480344405547054397038895817465368254916136220830268563778582290228416398307887896918556404084898937609373242171846359938695516765018940588109060426089671438864102814350385648747165832010614366132173102768902855220001
The output of a normal calculator for the above expression would be very different. We can simulate this with the numerical_approx
function which will return a numerical approximation of a value. So, if we want to compute the above value and see a decimal or scientific notation expression for the answer, we can do:
numerical_approx( (2/3)^1000 )
8.10477465652757e-177
Below we compute $5$ factorial. In SageMath
as with Python
any information on a line after #
is ignored. We use these to make comments explaining the code.
1*2*3*4*5 # 5 factorial
120
Of course, there is also a built in factorial
function in SageMath, so we can also compute this using:
factorial(5)
120
Some versions of division¶
With integer division, there are various variants. The single slash /
denotes normal division. It will return an exact quotient, which will typically be rational if the numerator and denominator are integers:
93/4
93/4
To perform integer division, you use two slashes:
93//4 # 93/4 rounded down to the nearest integer.
23
This always returns the greatest integer less than the fraction. So, for example:
-93//4
-24
We can also do modular arithmetic. The expression n % p
returns the number between $0$ (inclusive) and $p$ (exclusive) such that $n-k$ is an integer multiple of $p$.
5 % 3
2
-5 % 3
1
It is natural to want the integer quotient together with the remainder. To find this, you can use the divmod
function:
divmod(5,3)
(1, 2)
In the pair above, the first number is 5//3
and the second number is 5%3
.
Variables:¶
You can store a value into a variable using =
which is the assignment operator in both SageMath and Python.
Here we store the value 5
into the variable t
:
t = 5
Now we can access the value by using t
:
t
5
Aside: When we have multiple lines of code, only the last one is outputed. In the following code block, we first compute t+2
which is $7$, and then t+3
which is $8$. The $8$ is the result from the last computation, so it is printed out. The $7$ is discarded.
t+2
t+3
8
I find this is useful to tell me of the value of a variable after an assignment. For example:
t = 35//2
t
17
t^2
289
What happens in the code block below?
t = t+1
t
18
Answer: When evaluating the first line, t = t+1
, first t+1
is computed. Since $t=17$ currently, the result is $18$. Then this value is stored in the variable $t$. So now we have $t=18$. Then the second line is there to output the value of $t$.
Comparisons¶
The double equals sign ==
is for comparison. Usually the result will be either True
or False
. This is the syntax in both SageMath and Python.
t == 17
True
There are also order comparitors: >
, >=
, <
, and <=
:
t > 2
True
t >= 13
True
t < 7
False
t <= 8
False
Built in constants¶
Sage has some built in constants. The most important of these are $\pi$ and $e$.
pi
pi
numerical_approx(pi)
3.14159265358979
e
e
numerical_approx(e)
2.71828182845905
We can do arithmetic with these variables as well. Again, Sage does exact arithmetic when possible.
Below we do some calculations involving $\pi$.
pi^2
pi^2
pi^2+pi
pi + pi^2
pi*(pi+1)
pi*(pi + 1)
sin(pi/2)
1
sin(pi/4)
1/2*sqrt(2)
sin(pi/4)^2
1/2
sin(pi/8)
1/2*sqrt(-sqrt(2) + 2)
sin(pi/8)
1/2*sqrt(-sqrt(2) + 2)
t = sin(pi/8)
If you have an object, you can access its parent to find out what type of object it is.
t.parent()
Symbolic Ring
Above, we see that the parent of $t$ is Symbolic ring
. This means that $t$ is some algebraic expression in terms of functions, variables, and constants in SageMath. In this case it involved some square roots and the number $2$.
This is a bit of an aside.
Sage has other parents as well. The parent sin(pi/8)
is in Symbolic Ring
but we can convert it to a member of AA
, which is the field of all algebraic real numbers. (These are the roots of polynomial equations in one variable with rational coefficients.)
# Convert sin(pi/8) into the field of Algebraic real numbers
t = AA(sin(pi/8))
t
0.3826834323650898?
t.parent()
Algebraic Real Field
By definition of the algebraic real field, each number is associated with a polynomial. You can access this polynomial using the minpoly
method:
t.minpoly()
x^4 - x^2 + 1/8
Sage variables¶
Note that x
appears in the expression above. The quantity x
is a built in symbolic variable in Sage. I'll simply call such a thing a “sage variable”.
You can already use it in Sage:
x
x
x^2+x
x^2 + x
Note that x
is special. I think it is the only predefined sage variable. If you try to do the same sort of thing with y
you will get an error:
y
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[110], line 1 ----> 1 y NameError: name 'y' is not defined
But you can make y
into a sage variable using the var
function. Here we create a variable named y
and store it in the Python variable y
.
y = var('y')
y
y
x^2*y
x^2*y
We can also use the var
to create a variable in the middle of an expression:
x*y*var('z')
x*y*z
Calling var('z')
also creates a Python variable z
to store it. So, we can now also write the following:
x*y*z
x*y*z
I want to point out a distinction between these two types of variables (Python and Sage variables). Python variables store values which may well be sage objects, while Sage variables are used in symbolic expressions in Sage. By default, these things will be correlated, but it is possible to break this, which can lead to very weird errors.
For example, we can assign the Python variable z
to take the value var('y')
. Then when we evaluate z
, we get y
.
z = var('y')
z
y
Now we can set the Python variable y
equal to 18.
y = 18
y
18
With these crazy choices, we can do some things that don't seem to make sense:
y == 18
True
z == y
y == 18
Above the Python variable z
becomes the Sage variable y
and the Python variable y
becomes 18
. It can't evaluate it because the Sage variable y
is a variable which may or may not be equal to 18. So, rather than returning True
or False
, it returns the expression y == 18
.
If you force Sage to do so, it will return True
or False
. You can do this by converting the expression to a boolean value using the bool
function:
bool(z == y)
False
Above False
is returned because the expression y
and the expression 18
are not equal as symbolic expressions.
The following two expressions are equal as symbolic expressions:
x*(x+1) == x^2 + x
(x + 1)*x == x^2 + x
But again, you can't tell they are equal until you evaluate them with bool
:
bool(x*(x+1) == x^2 + x)
True